package at.fhooe.pandora.views;

import java.awt.Color;
import java.awt.geom.Point2D;

import at.fhooe.engine.event.events.*;
import at.fhooe.engine.mapobject.Wall;
import at.fhooe.engine.pandora.Game;
import at.fhooe.engine.physics.CircleBody;
import at.fhooe.engine.scenegraph.SceneNode;
import at.fhooe.engine.state.StateDataObject;
import at.fhooe.engine.view.View;
import at.fhooe.engine.visuals.*;
import at.fhooe.pandora.visuals.*;
import at.fhooe.pandora.entities.AsteroidEntity;
import at.fhooe.pandora.entities.BulletEntity;
import at.fhooe.pandora.entities.ResourceEntity;
import at.fhooe.pandora.entities.ShipEntity;

public class PlayView implements View, EntityMovedEvent.Listener, EntityCreatedEvent.Listener, EntityDestroyedEvent.Listener, MapObjectCreatedEvent.Listener , MapObjectDestroyedEvent.Listener{
	
	private double m_scaleX;
	private double m_scaleY;
	
	public PlayView(double scaleX, double scaleY) {
		m_scaleX = scaleX;
		m_scaleY = scaleY;
	}
	
	@Override
	public void engage(StateDataObject data) {
		engage();
	}
	
	public void engage() {
		addListeners();
		Game.getInstance().getGraphicsSystem().setScale(m_scaleX, m_scaleY);
	}

	public void disengage() {
		removeListeners();
		
		Game.getInstance().getGraphicsSystem().setScale(1.0, 1.0 );
		Game.getInstance().getSceneNodeManager().destroyAllSceneNodes();
	}
	
	private void addListeners() {
		Game.getInstance().getEventManager().addListener(this, EntityMovedEvent.TYPE);
		Game.getInstance().getEventManager().addListener(this, EntityCreatedEvent.TYPE);
		Game.getInstance().getEventManager().addListener(this, EntityDestroyedEvent.TYPE);
		Game.getInstance().getEventManager().addListener(this, MapObjectCreatedEvent.TYPE);
		Game.getInstance().getEventManager().addListener(this, MapObjectDestroyedEvent.TYPE);
	}
	
	private void removeListeners() {
		Game.getInstance().getEventManager().removeListener(this, EntityMovedEvent.TYPE);
		Game.getInstance().getEventManager().removeListener(this, EntityCreatedEvent.TYPE);
		Game.getInstance().getEventManager().removeListener(this, EntityDestroyedEvent.TYPE);
		Game.getInstance().getEventManager().removeListener(this, MapObjectCreatedEvent.TYPE);
		Game.getInstance().getEventManager().removeListener(this, MapObjectDestroyedEvent.TYPE);
	}
	
	public void onEntityCreatedEvent(EntityCreatedEvent entityCreatedEvent) {
		if (entityCreatedEvent.getEntityId().compareTo(ShipEntity.ID) == 0) {				// ship created
			createShipVisual(entityCreatedEvent.getEntityName());
		} else if (entityCreatedEvent.getEntityId().compareTo(AsteroidEntity.ID) == 0) {	// asteroid created
			createAsteroidVisual(entityCreatedEvent.getEntityName());
		} else if (entityCreatedEvent.getEntityId().compareTo(BulletEntity.ID) == 0) {		// bullet created
			createBulletVisual(entityCreatedEvent.getEntityName());
		} else if (entityCreatedEvent.getEntityId().compareTo(ResourceEntity.ID) == 0) {	// resource created
			createResourceVisual(entityCreatedEvent.getEntityName());
		} 
	}
	
	private void createWallVisual(String entityName) {
		SceneNode newNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName);
		
		if (newNode != null) {
			Wall wall = (Wall)Game.getInstance().getPathfindingManager().getMapObject(entityName);
			newNode.setVisual(new WallVisual(Color.blue, (int)wall.getSize(), true));
			newNode.transform(	wall.getPositionX(),
					wall.getPositionY(),
					0);
		}
	}

	private void createResourceVisual(String entityName) {
		SceneNode node = Game.getInstance().getSceneNodeManager().createSceneNode(entityName);
		
		if (node != null) {
			ResourceEntity resource = (ResourceEntity) Game.getInstance().getEntityManager().getEntity(entityName);
			if(resource.getType() == ResourceEntity.TYPE_GOLD) {
				node.setVisual(new StarVisual(Color.YELLOW.darker(), ResourceEntity.SIZE));
			} else if(resource.getType() == ResourceEntity.TYPE_COPPER) {
				node.setVisual(new StarVisual(Color.RED.darker(), ResourceEntity.SIZE));
			} else if(resource.getType() == ResourceEntity.TYPE_STONE) {
				node.setVisual(new StarVisual(Color.DARK_GRAY, ResourceEntity.SIZE));
			}
		}
	}

	private void createBulletVisual(String entityName) {
		SceneNode newNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName);
		
		if (newNode != null) {
			BulletEntity asteroid = (BulletEntity)Game.getInstance().getEntityManager().getEntity(entityName);
			newNode.setVisual(new CircleVisual(Color.RED, (int)asteroid.getSize(), true));
		}
	}

	private void createAsteroidVisual(String entityName) {
		SceneNode newNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName);
		
		if (newNode != null) {
			AsteroidEntity asteroid = (AsteroidEntity)Game.getInstance().getEntityManager().getEntity(entityName);
			newNode.setVisual(new CircleVisual(Color.ORANGE.darker(), (int)asteroid.getSize()*2, true));
//			newNode.setVisual(new StarVisual(asteroidColor, asteroid.getSize()));
		}
	}

	private void createShipVisual(String entityName) {	
		SceneNode newNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName);
		if (newNode != null) {
			newNode.setVisual(new ShipVisual(Color.PINK));
			SceneNode childNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName+"_BB");
			ShipEntity shipEntity = (ShipEntity)Game.getInstance().getEntityManager().getEntity(entityName);
			if (childNode != null){
				childNode.setVisual(new CircleVisual(Color.ORANGE.darker(), (int)((CircleBody)shipEntity.getPhysicBody()).getRadius()*2, false));
				newNode.addChild(childNode);
			}
//			childNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName+"_TractorBeam");
//			if (childNode != null){
//				childNode.setVisual(new CircleVisual(Color.GREEN.darker(), (int)shipEntity.getCollectingCircleBody().getRadius()*2, shipEntity.getCollectingCircleBody().getPositionX()-shipEntity.getPhysicBody().getPositionX(), shipEntity.getCollectingCircleBody().getPositionY()-shipEntity.getPhysicBody().getPositionY(), false));
//				newNode.addChild(childNode);
//			}
			childNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName+"_isectRayMiddle");
			if (childNode != null){
				childNode.setVisual(new LineVisual(Color.GREEN.brighter(), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP1(0), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP2(0)));
				newNode.addChild(childNode);
			}
			childNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName+"_isectRayLeft");
			if (childNode != null){
				childNode.setVisual(new LineVisual(Color.GREEN.brighter(), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP1(-1), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP2(-1)));
				newNode.addChild(childNode);
			}
			childNode = Game.getInstance().getSceneNodeManager().createSceneNode(entityName+"_isectRayRight");
			if (childNode != null){
				childNode.setVisual(new LineVisual(Color.GREEN.brighter(), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP1(1), shipEntity.getSteering().getObstacleAvoidance().getRelativeIsectLineP2(1)));
				newNode.addChild(childNode);
			}
		}
	}
	
	public void onEntityMovedEvent(EntityMovedEvent entityMovedEvent) {
		SceneNode moveNode = Game.getInstance().getSceneNodeManager().getSceneNode(entityMovedEvent.getEntityName());
		moveNode.transform(	entityMovedEvent.getNewPositionX(),
							entityMovedEvent.getNewPositionY(),
							entityMovedEvent.getNewRotationAngle());
	}
	
	public void onEntityDestroyedEvent(EntityDestroyedEvent entityDestroyedEvent) {
		Game.getInstance().getSceneNodeManager().destroySceneNode(entityDestroyedEvent.getEntityName());
	}

	public void onMapObjectDestroyedEvent(MapObjectDestroyedEvent mapObjectDestroyedEvent) {
		Game.getInstance().getSceneNodeManager().destroySceneNode(mapObjectDestroyedEvent.getMapObjectName());
	}

	public void onMapObjectCreatedEvent(MapObjectCreatedEvent mapObjectCreatedEvent) {
		//System.out.println("MapObjectCreatedEvent " + Wall.ID + " | " + mapObjectCreatedEvent.getMapObjectId());
		if (mapObjectCreatedEvent.getMapObjectId().compareTo(Wall.ID) == 0) {
			createWallVisual(mapObjectCreatedEvent.getMapObjectName());
		}
	}
	
	
	
}
